package mobileann.mafamily.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import mobileann.mafamily.service.UpdateService;
import mobileann.mafamily.utils.FileUtilsForDown.ProgressListener;

import com.mobileann.mafamily.R;
import com.mofind.android.base.L;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

/**
 * 功能:网络下载的工具类
 * <br>用途0:下载字符串
 * <br>用途1:下载apk
 * @annotation ADRIAN.2015.9.6.16.55
 */
public class HttpDownloader {
	private URL url = null;

	/**
	 * 根据URL下载文件,前提是这个文件当中的内容是文本,函数的返回值就是文本当中的内容
	 * <br>1.创建一个URL对象
	 * <br>2.通过URL对象,创建一个HttpURLConnection对象 
	 * <br>3.得到InputStream 
	 * <br>4.从InputStream当中读取数据
	 * @param urlStr
	 * @return
	 */
	public String download(String urlStr) {
		StringBuffer sb = new StringBuffer();
		String line = null;
		BufferedReader buffer = null;
		try {
			url = new URL(urlStr);
			HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
			buffer = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
			while ((line = buffer.readLine()) != null) {
				sb.append(line);
			}
		} catch (Exception e) {
			L.e("maf", "error--", e);
		} finally {
			try {
				buffer.close();
			} catch (IOException e) {
				L.e("maf", "error--", e);
			}
		}
		return sb.toString();
	}

	/**
	 * 
	 * @param urlStr
	 * @param path
	 * @param fileName
	 * @return -1:文件下载出错 0:文件下载成功
	 * @throws IOException
	 */
	public int downFile(String urlStr, String path, String fileName)
			throws IOException {
		InputStream inputStream = null;
		{
			FileUtilsForDown fileUtils = new FileUtilsForDown();
			// 如果已经存在该文件, 先将文件删除
			if (fileUtils.isFileExist(path + fileName)) {
				fileUtils.deleteFile(path + fileName);
			}
			inputStream = getInputStreamFromURL(urlStr);
			File resultFile = fileUtils.write2FileFromInput(path, fileName,
					inputStream);
			if (resultFile == null) {
				return -1;
			}
		}
		return 0;
	}
	
	/**
	 * 功能:更新文件
	 * @param urlStr
	 * @param path
	 * @param fileName
	 * @param listener
	 * @return
	 * @throws IOException
	 * @annotation ADRIAN.2015.9.6.17.13
	 */
	public int updateFile(String urlStr, String path, String fileName,final ProgressListener listener)
			throws IOException {
		InputStream inputStream = null;
		{
			FileUtilsForDown fileUtils = new FileUtilsForDown();
			if (fileUtils.isFileExist(path + fileName)) {
				fileUtils.deleteFile(path + fileName);
			}
			inputStream = getInputStreamFromURL(urlStr);
			File resultFile = fileUtils.write3FileFromInput(path, fileName,
					inputStream,url.openConnection().getContentLength(),listener);
			if (resultFile == null) {
				return -1;
			}
		}
		return 0;
	}
	

	public int downFiletoself(String urlStr, String path, String fileName,
			Handler handler) throws IOException {
		InputStream inputStream = null;
		{
			FileUtilsForDown fileUtils = new FileUtilsForDown();
			if (fileUtils.isFileExist(path + fileName)) {
				fileUtils.deleteFile(path + fileName);
			}
			inputStream = getInputStreamFromURL(urlStr);
			File resultFile = fileUtils.write2FileFromInput(path, fileName,
					inputStream);
			if (resultFile == null) {
				return -1;
			}
		}
		return 0;
	}

	/**
	 * 根据URL得到输入流
	 * 
	 * @param urlStr
	 * @return
	 * @throws IOException
	 */
	public InputStream getInputStreamFromURL(String urlStr) throws IOException {
		HttpURLConnection urlConn = null;
		InputStream inputStream = null;
		{
			url = new URL(urlStr);
			urlConn = (HttpURLConnection) url.openConnection();
			inputStream = urlConn.getInputStream();
		}
		return inputStream;
	}
}
